package com.ogawa.joypal.user.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.ogawa.joypal.user.R;
import com.ogawa.joypal.user.util.TimeUtil;


import java.util.Calendar;

/**
 * 生日选择弹窗
 */
public class DatePickDialog extends BottomSheetDialog {
    private NumberPickerView yearView;
    private NumberPickerView monthView;
    private NumberPickerView dayView;
    private int year = 1970;
    private int month = 1;
    private int day = 1;
    private TextView tvSave;
    private String[] years;
    private String[] months;
    private String[] days;


    public DatePickDialog(@NonNull Context context) {
        super(context);
        getWindow().addFlags(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.dialog_birthday);
        FrameLayout bottomSheet = (FrameLayout) getDelegate().findViewById(R.id.design_bottom_sheet);
        bottomSheet.setBackgroundColor(Color.TRANSPARENT);
        final TimeUtil timeUtil = new TimeUtil(getContext());
        yearView = findViewById(R.id.np_year);
        monthView = findViewById(R.id.np_month);
        dayView = findViewById(R.id.np_day);
        tvSave = findViewById(R.id.tv_save);
        findViewById(R.id.tv_cancel).setOnClickListener(view -> dismiss());
        months = getContext().getResources().getStringArray(R.array.array_month);
        int yearLen = Calendar.getInstance().get(Calendar.YEAR) - 1970 + 1;
        years = new String[yearLen];
        for (int i = 0; i < yearLen; i++) {
            years[i] = String.valueOf(1970 + i);
        }
        yearView.setDisplayedValues(years);
        yearView.setMinValue(0);
        yearView.setMaxValue(yearLen - 1);
        yearView.setValue(0);
        monthView.setValue(0);
        days = timeUtil.changeDay(dayView, year, month);
        yearView.setOnValueChangedListener((picker, oldValue, newValue) -> {
            year = Integer.parseInt(years[newValue]);
            months = timeUtil.changeMonth(monthView, year);
            days = timeUtil.changeDay(dayView, year, month);
        });
        monthView.setOnValueChangedListener((picker, oldValue, newValue) -> {
            month = Integer.parseInt(months[newValue]);
            days = timeUtil.changeDay(dayView, year, month);
        });
        dayView.setOnValueChangedListener((picker, oldValue, newValue) -> day = Integer.parseInt(days[newValue]));
    }

    public void setTvSaveOnClickListener(View.OnClickListener listener) {
        tvSave.setOnClickListener(listener);
    }

    public int getYear() {
        return Integer.valueOf(years[yearView.getValue()]);
    }

    public int getMonth() {
        return Integer.valueOf(months[monthView.getValue()]);
    }

    public int getDay() {
        return Integer.valueOf(days[dayView.getValue()]);
    }
}
